package com.atguigu.tingshu.payment.service.impl;

import cn.hutool.core.util.IdUtil;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.payment.PaymentInfo;
import com.atguigu.tingshu.payment.config.WxPayV3Config;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.atguigu.tingshu.payment.service.WxPayService;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.*;
import com.wechat.pay.java.service.payments.model.Transaction;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WxPayServiceImpl implements WxPayService {

	@Autowired
	private PaymentInfoService paymentInfoService;

	@Autowired
	private RSAAutoCertificateConfig rsaAutoCertificateConfig;

	@Autowired
	private WxPayV3Config wxPayV3Config;

	@Override
	public Map<String, Object> createJsapi(Long userId,String paymentType, String orderNo) {
		PaymentInfo paymentInfo = paymentInfoService.savePaymentInfo(userId,paymentType,orderNo);
		//2.调用微信SDK获取小程序支付所需参数
		//2.1 构建调用微信接口业务对象
		JsapiServiceExtension jsapiService = new JsapiServiceExtension.Builder().config(rsaAutoCertificateConfig).build();
		//2.2 构建预支付请求对象
		PrepayRequest prepayRequest = new PrepayRequest();
		Amount amount = new Amount();
		//2.2.1 设置预支付订单金额 单位：分 TODO开发接口暂时硬编码1分 实际应该从paymentInfo对象中获取
		amount.setTotal(1);
		prepayRequest.setAmount(amount);
		prepayRequest.setAppid(wxPayV3Config.getAppid());
		prepayRequest.setMchid(wxPayV3Config.getMerchantId());
		prepayRequest.setDescription("商品：" + paymentInfo.getContent());
		prepayRequest.setNotifyUrl(wxPayV3Config.getNotifyUrl());
		prepayRequest.setOutTradeNo(orderNo);
		Payer payer = new Payer();
		payer.setOpenid("odo3j4qp-wC3HVq9Z_D9C0cOr0Zs");
		prepayRequest.setPayer(payer);
		prepayRequest.setOutTradeNo(paymentInfo.getOrderNo());
		PrepayWithRequestPaymentResponse response = jsapiService.prepayWithRequestPayment(prepayRequest);
		if(response != null){
			Map<String, Object> mapResult = new HashMap<>();
			mapResult.put("timeStamp", response.getTimeStamp());
			mapResult.put("package", response.getPackageVal());
			mapResult.put("paySign", response.getPaySign());
			mapResult.put("signType", response.getSignType());
			mapResult.put("nonceStr", response.getNonceStr());
			return mapResult;
		}
		return Map.of();
	}

	@Override
	public Boolean queryPayStatus(String orderNo) {
//		//1.创建查询交易请求对象
//		QueryOrderByOutTradeNoRequest queryOrderByOutTradeNoRequest = new QueryOrderByOutTradeNoRequest();
//		queryOrderByOutTradeNoRequest.setMchid(wxPayV3Config.getMerchantId());
//		queryOrderByOutTradeNoRequest.setOutTradeNo(orderNo);
//
//		//2.创建调用微信服务端业务对象
//		JsapiServiceExtension jsapiService = new JsapiServiceExtension.Builder().config(rsaAutoCertificateConfig).build();
//
//		//3.调用微信查询交易状态接口
//		Transaction transaction = jsapiService.queryOrderByOutTradeNo(queryOrderByOutTradeNoRequest);
//		if(transaction != null){
//			Transaction.TradeStateEnum tradeState = transaction.getTradeState();
//			if(Transaction.TradeStateEnum.SUCCESS == tradeState){
//				return true;
//			}
//		}
//		return false;
//		//1.伪造微信交易对象
		Transaction transaction = new Transaction();
		transaction.setOutTradeNo(orderNo);
		transaction.setTransactionId("WX"+ IdUtil.getSnowflakeNextId());
		//修改支付记录状态
		paymentInfoService.updatePaymentInfoSuccess(transaction);
		return true;
	}

	@Override
	public Map<String, String> paySuccessNotify(HttpServletRequest request) {
		return Map.of();
	}
}
